Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 302)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.53688 11.57460 11.61203 11.64917 11.68600 11.72254 11.75879 11.79475
##   [9] 11.83041 11.86579 11.90087 11.93567 11.97019 12.00440 12.03830 12.07191
##  [17] 12.10522 12.13824 12.17100 12.20349 12.23572 12.26771 12.29947 12.33101
##  [25] 12.36229 12.39332 12.42406 12.45450 12.48463 12.51441 12.54385 12.57291
##  [33] 12.60158 12.62985 12.65844 12.68778 12.71738 12.74673 12.77534 12.80271
##  [41] 12.82834 12.85439 12.88271 12.91235 12.94237 12.97182 12.99975 13.02522
##  [49] 13.04727 13.06495 13.08129 13.09913 13.11726 13.13448 13.14958 13.16138
##  [57] 13.16865 13.17220 13.17366 13.17306 13.17046 13.16589 13.15938 13.15098
##  [65] 13.14072 13.12865 13.11480 13.09921 13.08193 13.06299 13.03780 13.00418
##  [73] 12.96579 12.92630 12.88937 12.85864 12.82888 12.79346 12.75401 12.71213
##  [81] 12.66945 12.62759 12.58814 12.55274 12.52300 12.49624 12.46894 12.44137
##  [89] 12.41385 12.38666 12.36012 12.33451 12.31014 12.28730 12.26630 12.24743
##  [97] 12.23098 12.21772 12.20775 12.20039 12.19497 12.19082 12.18726 12.18362
## [105] 12.17922 12.17339 12.16778 12.16416 12.16202 12.16085 12.16014 12.15939
## [113] 12.15809 12.15651 12.15528 12.15445 12.15403 12.15404 12.15453 12.15552
## [121] 12.15703 12.15910 12.16175 12.16501 12.16891 12.17387 12.18002 12.18697
## [129] 12.19433 12.20172 12.20873 12.21497 12.22275 12.23390 12.24742 12.26227
## [137] 12.27742 12.29183 12.30449 12.31436 12.32042 12.32233 12.32093 12.31690
## [145] 12.31091 12.30366 12.29583 12.28811 12.28117 12.27571 12.27241 12.27195
## [153] 12.27502 12.27777 12.27683 12.27357 12.26940 12.26570 12.26386 12.26526
## [161] 12.27131 12.28338 12.30122 12.32319 12.34862 12.37684 12.40717 12.43895
## [169] 12.47149 12.50414 12.53621 12.56703 12.59594 12.62225 12.65110 12.68723
## [177] 12.72942 12.77645 12.82710 12.88013 12.93432 12.98846 13.04132 13.09166
## [185] 13.13827 13.17993 13.21541 13.24348 13.26293 13.27252 13.27304 13.26707
## [193] 13.25604 13.24142 13.22466 13.20722 13.19053 13.17006 13.14111 13.10498
## [201] 13.06296 13.01633 12.96638 12.91441 12.86168 12.80951 12.75916 12.71193
## [209] 12.66911 12.62569 12.57715 12.52550 12.47276 12.42094 12.37206 12.32813
## [217] 12.28692 12.24507 12.20292 12.16076 12.11894 12.07775 12.03754 11.99860
## [225] 11.96128 11.92472 11.88812 11.85181 11.81614 11.78145 11.74809 11.71639
## [233] 11.68514 11.65318 11.62090 11.58872 11.55703 11.52624 11.49676 11.46898
## [241] 11.44330 11.42015 11.39990 11.38298 11.36790 11.35325 11.33956 11.32736
## [249] 11.31715 11.30948 11.30485 11.30230 11.30068 11.30025 11.30128 11.30402
## [257] 11.30874 11.31570 11.32515 11.33736 11.35533 11.38038 11.41030 11.44285
## [265] 11.47582 11.50698 11.53411 11.56214 11.59653 11.63585 11.67868 11.72359
## [273] 11.76915 11.81392 11.85649 11.89542 11.92929 11.95666 11.97612 11.99170
## [281] 12.00778 12.02337 12.03748 12.04912 12.05732 12.06108 12.06160 12.06058
## [289] 12.05781 12.05309 12.04621 12.03696 12.02513 12.01052 11.99293 11.97201
## [297] 11.94775 11.92036 11.89009 11.85716 11.82180 11.78426
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 302)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.70093 10.79558 10.88826 10.97895 11.06765 11.15434 11.23902 11.32168
##   [9] 11.40231 11.48091 11.55746 11.63195 11.70438 11.77469 11.84287 11.90895
##  [17] 11.97296 12.03495 12.09494 12.15298 12.20911 12.26335 12.31542 12.36509
##  [25] 12.41244 12.45758 12.50062 12.54164 12.58076 12.61808 12.65369 12.68770
##  [33] 12.72020 12.75131 12.78034 12.80681 12.83108 12.85350 12.87444 12.89424
##  [41] 12.91327 12.92985 12.94252 12.95199 12.95895 12.96411 12.96816 12.97181
##  [49] 12.97576 12.98071 12.98455 12.98531 12.98392 12.98131 12.97838 12.97608
##  [57] 12.97532 12.97508 12.97380 12.97158 12.96856 12.96483 12.96052 12.95575
##  [65] 12.95062 12.94525 12.93976 12.93425 12.92886 12.92369 12.91985 12.91763
##  [73] 12.91588 12.91343 12.90914 12.90187 12.89298 12.88445 12.87598 12.86729
##  [81] 12.85807 12.84805 12.83693 12.82442 12.81024 12.79452 12.77766 12.75981
##  [89] 12.74109 12.72162 12.70153 12.68095 12.66001 12.63884 12.61756 12.59629
##  [97] 12.57517 12.55382 12.53174 12.50886 12.48515 12.46054 12.43499 12.40844
## [105] 12.38083 12.35212 12.31907 12.28011 12.23769 12.19426 12.15226 12.11415
## [113] 12.08236 12.05152 12.01559 11.97603 11.93431 11.89189 11.85022 11.81077
## [121] 11.77500 11.74437 11.72035 11.70438 11.69794 11.69925 11.70497 11.71415
## [129] 11.72589 11.73925 11.75330 11.76712 11.78627 11.81527 11.85161 11.89279
## [137] 11.93632 11.97968 12.02038 12.05592 12.08379 12.10970 12.14041 12.17498
## [145] 12.21246 12.25193 12.29243 12.33304 12.37281 12.41081 12.44609 12.47772
## [153] 12.50476 12.52663 12.54416 12.55850 12.57083 12.58231 12.59410 12.60737
## [161] 12.62330 12.64303 12.66485 12.68627 12.70734 12.72810 12.74859 12.76884
## [169] 12.78889 12.80880 12.82858 12.84829 12.86797 12.88764 12.90943 12.93498
## [177] 12.96363 12.99472 13.02759 13.06158 13.09602 13.13027 13.16366 13.19552
## [185] 13.22521 13.25205 13.27540 13.29459 13.30895 13.31784 13.32270 13.32546
## [193] 13.32620 13.32499 13.32190 13.31700 13.31038 13.30143 13.28966 13.27530
## [201] 13.25856 13.23966 13.21883 13.19629 13.17225 13.14695 13.12058 13.09339
## [209] 13.06559 13.03773 13.00987 12.98146 12.95196 12.92082 12.88749 12.85144
## [217] 12.80934 12.75955 12.70404 12.64477 12.58372 12.52286 12.46416 12.40958
## [225] 12.36109 12.31105 12.25286 12.19010 12.12635 12.06521 12.01023 11.96502
## [233] 11.92665 11.88979 11.85434 11.82022 11.78733 11.75559 11.72491 11.69520
## [241] 11.66637 11.63834 11.61102 11.58431 11.55749 11.53040 11.50382 11.47853
## [249] 11.45530 11.43492 11.41816 11.40505 11.39483 11.38711 11.38150 11.37762
## [257] 11.37508 11.37348 11.37245 11.37159 11.36896 11.36392 11.35807 11.35297
## [265] 11.35020 11.35135 11.35797 11.37026 11.38686 11.40708 11.43024 11.45565
## [273] 11.48262 11.51047 11.53850 11.56603 11.59237 11.61683 11.63873 11.66055
## [281] 11.68470 11.71033 11.73662 11.76274 11.78785 11.81113 11.83359 11.85671
## [289] 11.88028 11.90414 11.92808 11.95194 11.97551 11.99862 12.02108 12.04261
## [297] 12.06315 12.08293 12.10215 12.12100 12.13969 12.15843
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 302)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.80010 10.86873 10.93597 11.00177 11.06611 11.12897 11.19031 11.25011
##   [9] 11.30834 11.36496 11.41996 11.47330 11.52495 11.57488 11.62311 11.66971
##  [17] 11.71478 11.75838 11.80060 11.84152 11.88121 11.91975 11.95610 11.98940
##  [25] 12.02003 12.04834 12.07471 12.09950 12.12308 12.14582 12.16807 12.19022
##  [33] 12.21261 12.23563 12.25860 12.28068 12.30205 12.32283 12.34319 12.36328
##  [41] 12.38324 12.40409 12.42637 12.44948 12.47285 12.49589 12.51802 12.53864
##  [49] 12.55719 12.57307 12.58820 12.60433 12.62056 12.63601 12.64980 12.66106
##  [57] 12.66889 12.67390 12.67733 12.67928 12.67982 12.67904 12.67704 12.67389
##  [65] 12.66969 12.66452 12.65847 12.65162 12.64407 12.63590 12.62484 12.60966
##  [73] 12.59198 12.57346 12.55573 12.54041 12.52553 12.50832 12.48926 12.46881
##  [81] 12.44745 12.42565 12.40388 12.38262 12.36233 12.34104 12.31690 12.29059
##  [89] 12.26276 12.23408 12.20522 12.17684 12.14961 12.12419 12.10125 12.08145
##  [97] 12.06546 12.05236 12.04065 12.03015 12.02065 12.01197 12.00391 11.99628
## [105] 11.98888 11.98153 11.97593 11.97321 11.97236 11.97234 11.97215 11.97075
## [113] 11.96711 11.96306 11.96084 11.96013 11.96057 11.96183 11.96357 11.96544
## [121] 11.96711 11.96824 11.96849 11.96752 11.96498 11.96030 11.95367 11.94593
## [129] 11.93789 11.93039 11.92426 11.92031 11.91710 11.91280 11.90761 11.90174
## [137] 11.89541 11.88883 11.88221 11.87578 11.86974 11.86227 11.85192 11.83948
## [145] 11.82572 11.81141 11.79734 11.78426 11.77297 11.76424 11.75883 11.75754
## [153] 11.76113 11.76754 11.77439 11.78197 11.79057 11.80048 11.81199 11.82539
## [161] 11.84098 11.85903 11.88054 11.90591 11.93453 11.96578 11.99908 12.03381
## [169] 12.06936 12.10514 12.14054 12.17495 12.20777 12.23839 12.27133 12.31079
## [177] 12.35574 12.40516 12.45799 12.51321 12.56978 12.62666 12.68281 12.73721
## [185] 12.78880 12.83656 12.87946 12.91644 12.94649 12.96855 12.98492 12.99857
## [193] 13.00953 13.01782 13.02346 13.02648 13.02688 13.02364 13.01601 13.00445
## [201] 12.98942 12.97138 12.95078 12.92810 12.90377 12.87827 12.85206 12.82558
## [209] 12.79931 12.77099 12.73875 12.70359 12.66655 12.62864 12.59088 12.55430
## [217] 12.51374 12.46493 12.41015 12.35170 12.29187 12.23295 12.17723 12.12701
## [225] 12.08458 12.04275 11.99474 11.94349 11.89195 11.84306 11.79976 11.76500
## [233] 11.73631 11.70933 11.68414 11.66080 11.63938 11.61996 11.60259 11.58736
## [241] 11.57434 11.56359 11.55518 11.54919 11.55076 11.56253 11.58083 11.60196
## [249] 11.62224 11.63800 11.64554 11.65162 11.66421 11.68141 11.70136 11.72219
## [257] 11.74200 11.75893 11.77110 11.77664 11.77493 11.76797 11.75773 11.74620
## [265] 11.73535 11.72717 11.72364 11.72105 11.71497 11.70631 11.69597 11.68488
## [273] 11.67393 11.66404 11.65611 11.65106 11.64978 11.65320 11.66222 11.67419
## [281] 11.68627 11.69912 11.71339 11.72973 11.74879 11.77123 11.79622 11.82263
## [289] 11.85063 11.88037 11.91200 11.94569 11.98159 12.01986 12.06066 12.10416
## [297] 12.15033 12.19902 12.25006 12.30331 12.35860 12.41580
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")